package com.tyk.bookstore.front.message.controller;


import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.common.core.model.vo.PageVo;
import com.tyk.bookstore.front.message.model.query.ServiceNoticeListQuery;
import com.tyk.bookstore.front.message.model.query.ServiceNoticePageQuery;
import com.tyk.bookstore.front.message.model.vo.ServiceNoticeListVo;
import com.tyk.bookstore.front.message.model.vo.ServiceNoticePageVo;
import com.tyk.bookstore.front.message.model.vo.ServiceNoticeVo;
import com.tyk.bookstore.front.message.service.ServiceNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tyk
 * @description 服务公告管理
 */
@RequestMapping({"api/message/notice/service"})
@Tag(name = "消息管理/服务公告管理", description = "服务公告管理")
@RestController
@RequiredArgsConstructor
public class ServiceNoticeController {
    private final ServiceNoticeService serviceNoticeService;

    @GetMapping({"{id}"})
    @Operation(summary = "获取服务公告详情", description = "获取服务公告详情")
    public R<ServiceNoticeVo> getServiceNoticeById(
            @Schema(description = "公告ID")
            @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(serviceNoticeService.getServiceNoticeById(id));
    }

    @GetMapping({"list"})
    @Operation(summary = "查询服务公告列表", description = "查询服务公告列表(首页)")
    public R<List<ServiceNoticeListVo>> getList(
            @Validated @ParameterObject ServiceNoticeListQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(new ArrayList<>()) : R.ok(serviceNoticeService.getList(query));
    }

    @GetMapping({"page"})
    @Operation(summary = "分页查询服务公告", description = "分页查询服务公告")
    public R<ServiceNoticePageVo> getPage(
            @Validated @ParameterObject ServiceNoticePageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(ServiceNoticePageVo.class)) : R.ok(serviceNoticeService.getPage(query));
    }
}
